﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using ebc.componentbinder.Contract;
using ebc.componentbinder.Helpers;

namespace ebc.componentbinder
{
    public partial class ComponentBinder: IComponentBinder
    {
        private const string LOG_FILENAME = "ebc.componentbinder.log";


        public void Bind(params object[] components)
        {
            Bind(components, null);
        }


        public void Bind(object[] components, Action<object> listener)
        {
            var mi = new StackTrace().GetFrame(1).GetMethod();
            File.AppendAllText(LOG_FILENAME, string.Format("\r\n:::Binding @ {0}, called by: {1}.{2}()\r\n", DateTime.Now, mi.DeclaringType.FullName, mi.Name));

            PairUpPins(components)
                .ForAll(pair =>
                {
                    File.AppendAllText(LOG_FILENAME,
                                       string.Format("{0}  ->  {1}\r\n", pair.Out, pair.In));
                    WireUpPins(pair, listener);
                });
        }
    }
}
